package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 会员
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    /**
     * 根据电话查询会员
     *
     * @param telephone 会员电话
     * @return 会员信息
     */
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    /**
     * 添加会员
     *
     * @param member 会员信息
     */
    @Override
    public void add(Member member) {

        //判断密码,需要加密
        String password = member.getPassword();
        if (password != null) {
            //使用MD5将明文进行加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }

        memberDao.add(member);
    }

    /**
     * 查询每个月,总会员数
     *
     * @param months 月份
     * @return 每个月份的总会员数量
     */
    @Override
    public List<Integer> findMemberCountByMonth(List<String> months) {

        List<Integer> list = new ArrayList<>();

        //遍历月份
        for (String month : months) {
            //拼接成格式 如:2020.05.31
            month += ".31";
            Integer count = memberDao.findMemberCountByBeforeDate(month);
            list.add(count);
        }

        return list;
    }

    /**
     * 查询每天 总会员数
     *
     * @return 每天 总会员数
     */
    @Override
    public List<Map<String, String>> getMemberReportByEveryday() {
        return memberDao.getMemberReportByEveryday();
    }


}
